package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ExChangeListBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.MyIntegralRechargeAdapter;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

public class MyIntegralRechargeActivity extends AppCompatActivity {

    int page = 1;
    String userid;

    List<ExChangeListBean.DataBean> list;
    MyIntegralRechargeAdapter adapter;
    LoadingDialog dialog;
    UserPresenter presenter;

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.rv_intergral_rechare)
    RecyclerView mRvIntergralRechare;
    @BindColor(R.color.white)
    int white;
    @Bind(R.id.refresh)
    SwipeRefreshLayout refresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_integral_recharge);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);

        dialog = new LoadingDialog(this);
        dialog.show();

        UserBean.DataEntity userbean = new UserBean.DataEntity();
        userbean.readInfoFromLocal(this);
        userid = userbean.getUserid();

        initToolbar();
        initList();

        presenter = new UserPresenterImp();
        presenter.getExchargeList(page, userid);
    }

    public void onEventMainThread(ExChangeListBean bean) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
        if (bean.getReturn_code() == 0) {
            if (bean.getData().isEmpty()) {
                DeviceUtil.MyToast(this, "已经是最后一页");
            } else {
                list.addAll(bean.getData());
                adapter.notifyDataSetChanged();
            }
        }
    }

    private void initToolbar() {
        mToolbar.setTitle("兑换记录");
        mToolbar.setTitleTextColor(white);
        mToolbar.setNavigationIcon(R.mipmap.btn_exit);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initList() {
        list = new ArrayList<>();
        adapter = new MyIntegralRechargeAdapter(list);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        mRvIntergralRechare.setLayoutManager(manager);
        mRvIntergralRechare.setAdapter(adapter);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                adapter.notifyDataSetChanged();

                page = 1;
                presenter.getExchargeList(page, userid);
            }
        });

        mRvIntergralRechare.addOnScrollListener(new RecyclerView.OnScrollListener() {
            //用来标记是否正在向最后一个滑动，既是否向右滑动或向下滑动
            boolean isSlidingToLast;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totalItemCount = manager.getItemCount();

                    // 判断是否滚动到底部，并且是向右滚动
                    if (lastVisibleItem == (totalItemCount - 1) && isSlidingToLast) {
                        //加载更多功能的代码
                        page++;
                        presenter.getExchargeList(page, userid);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //dx用来判断横向滑动方向，dy用来判断纵向滑动方向
                if (dy > 0) {
                    //大于0表示，正在向右滚动
                    isSlidingToLast = true;
                } else {
                    //小于等于0 表示停止或向左滚动
                    isSlidingToLast = false;
                }
            }
        });

        adapter.setOnClickListener(new MyIntegralRechargeAdapter.OnClickListener() {
            @Override
            public void click(String url, int position, String id) {
                Intent intent = new Intent(MyIntegralRechargeActivity.this, IntegralProductActivity.class);
                intent.putExtra(Constant.TO_SHOP_DETAIL_URL, url);
                intent.putExtra(Constant.TO_SHOP_DETAIL_STATUS, 1);
                intent.putExtra(Constant.TO_SHOP_DETAIL_ID, id);
                startActivity(intent);
            }
        });
    }
}
